package cn.com.jslh.boarpro.main.presenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.mvp.BaseListPresenter;
import cn.com.jslh.boarpro.base.mvp.BaseListView;
import cn.com.jslh.boarpro.main.model.InfoShowModelImpl;
import cn.com.jslh.boarpro.main.view.fragment.InfoShowFragment;
import cn.com.jslh.boarpro.util.WebService;

/**
 * 作者：周洲 on 2017/7/31 08:46
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class InfoShowPresenterImpl extends BaseListPresenter {
    private SlimAdapterEx infoAdapter;

    public InfoShowPresenterImpl(Context context, BaseListView baseListView) {
        super(context, baseListView, new InfoShowModelImpl(context));
    }

    @Override
    public void upDatas(String... param) {

    }

    public void upAsyncDatas(final InfoShowFragment.getInfoSuccessListener infoSuccessListener, final String... params){
        if(params[0].equals(InfoShowFragment.Clear)){
            infoAdapter.updateData(new ArrayList<>()).notifyDataSetChanged();
            return;
        }
        ((InfoShowModelImpl)baseListModel).getAsyncMuZhuDatas(params[0], new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                List<String> results = new ArrayList<String>();
                JSONObject jsonObject = new JSONObject(s);
                String flag =jsonObject.getString("flag");
                if(flag.equals("1")){
                    JSONArray jsonArray= jsonObject.getJSONArray("array");
                    if(jsonArray.length()>0) {
                        JSONObject data = jsonArray.getJSONObject(0);
                        JSONArray names = data.names();
                        for (int i = 0; i < names.length(); i++) {
                            String key = names.getString(i);
                            if (!key.endsWith("ID")) {
                                String value = data.getString(names.getString(i));
                                results.add(value);
                            }
                        }
                        if(params[0].equals(InfoShowFragment.DuanNaiType)){
                            infoSuccessListener.success(s, data.getString("FZZID"));
                        }else {
                            infoSuccessListener.success(params[1], data.getString("FZZID"));
                        }
                    }else {
                        Toast.makeText(context, "服务器中无此耳号记录信息", Toast.LENGTH_SHORT).show();
                    }
                    infoAdapter.updateData(results).notifyDataSetChanged();
//                    infoSuccessListener.success(params[0],);
                }else {
                    Toast.makeText(context, jsonObject.getString("warn"), Toast.LENGTH_SHORT).show();
                }
            }
        }, params);
    }

    @Override
    public void setUI(RecyclerView recyclerView, String... param) {
        infoAdapter = SlimAdapter.create(SlimAdapterEx.class)
                .enableDiff()
                .registerDefault(R.layout.add_breeding_info_list_item, new SlimInjector<String>() {
                    @Override
                    public void onInject(String s, IViewInjector injector) {
                        injector.text(R.id.title, s);
                    }
                }).attachTo(recyclerView);
    }
}
